﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TradingAppDal;
using TradingApp.Helper;

namespace TradingApp.Admin
{
    public partial class frmPoolInterval : Form
    {
        public frmPoolInterval()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidatePoolInterval())
                {
                    TradingAppSettings.PoolIntervalInSecs = Convert.ToInt32(tbPoolInterval.Text.Trim());
                    // change timer pool interval as well
                    ChangeMainAppTimer(TradingAppSettings.PoolInterval);
                    // save in xml
                    XmlHelper.SetTagValue(TradingAppSettings.CONN_ATTRIBUTE_STR,TradingAppSettings.PoolIntervalInSecs.ToString());
                    MessageBox.Show("Pool Interval successfully changed.", "Pool Interval", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void ChangeMainAppTimer(int poolInterval)
        {
            frmTradingAppParent ofrmTradingAppParent = (frmTradingAppParent)this.Tag;
            ofrmTradingAppParent.MainAppInstance.SetTimeInterval(poolInterval);
        }

        private bool ValidatePoolInterval()
        {
            int iPoolInterval;
            if (String.IsNullOrEmpty(tbPoolInterval.Text.Trim()))
            {
                MessageBox.Show(" Pool Interval cannot be empty.", "Pool Interval", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (!Int32.TryParse(tbPoolInterval.Text.Trim(), out iPoolInterval))
            {
                MessageBox.Show(" It is not a valid integer.", "Pool Interval", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void frmPoolInterval_Load(object sender, EventArgs e)
        {
            tbPoolInterval.Text = TradingAppSettings.PoolIntervalInSecs.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
